#include "TileSystem.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include <vector>

CTileSystem::CTileSystem(void)
{
}


CTileSystem::~CTileSystem(void)
{
}

void CTileSystem::Render( void )
{
	RECT rSource = { };
	for(unsigned int i = 0; i < m_vGrid.size(); ++i)
	{
		for(unsigned int j = 0; j < m_vGrid[0].size(); ++j)
		{
			rSource.left	= (m_vGrid[i][j]%(m_nTileSetWidth)) * m_nTileWidth;
			rSource.right	= rSource.left + m_nTileWidth;
			rSource.top		= (m_vGrid[i][j]/(m_nTileSetWidth)) * m_nTileHeight;
			rSource.bottom	= rSource.top + m_nTileHeight;

			CSGD_TextureManager::GetInstance()->Draw( m_nImageID, m_nTileWidth*i, m_nTileHeight*j,
				1.0f,1.0f,&rSource);
		}
	}
}

unsigned int CTileSystem::GetGridHeight( ) const
{
	//CAUTION! calling this without setting the grid size will crash!
	return m_vGrid[0].size();
}
unsigned int CTileSystem::GetGridWidth(	) const
{
	return m_vGrid.size();
}
void CTileSystem::SetGridSize(unsigned int _unWidth, unsigned int _unHeight)
{
	m_vGrid.resize(_unWidth, std::vector<int>(_unHeight, 0));
}

void CTileSystem::SetTile(unsigned int _nX, unsigned int _nY, unsigned int _nIndex)
{
	//Caution! This will act strangely if _nIndex is greater than the total number of tiles!
	m_vGrid[_nX][_nY] = _nIndex;
}

void CTileSystem::SetAllTiles(unsigned int _nIndex)
{
	//Caution! This will act strangely if _nIndex is greater than the total number of tiles!
	for(unsigned int i = 0; i < m_vGrid.size(); ++i)
	{
		for(unsigned int j = 0; j < m_vGrid[0].size(); ++j)
		{
			m_vGrid[i][j] = _nIndex;
		}
	}
}